﻿
using IBLL;
using IDAL;
using Model;

namespace BLL
{
    public class userbll:Iuserbll
    {
        public userbll(IBasedal<user> dal,IBasedal<roomfh> dal1,IBasedal<roomfx> dal2)
        {
            Dal = dal;
            Dal1 = dal1;
            Dal2 = dal2;
        }

        public IBasedal<user> Dal { get; }
        public IBasedal<roomfh> Dal1 { get; }
        public IBasedal<roomfx> Dal2 { get; }

        public Task<int> add(user user)
        {
            return Dal.add(user);
        }

        public dynamic fhxs()
        {
            return Dal1.xs();
        }

        public dynamic fxxs()
        {
           return Dal2.xs();
        }

        public int login(string zh, string mm)
        {
            var list = Dal.xs().FirstOrDefault(x => x.zh == zh);
            if (list != null)
            {
                 list = Dal.xs().FirstOrDefault(x => x.mm == mm);
                if (list != null)
                {
                    return 1;
                }

                return -1;
            }
            return -2;
        }

        public Task<int> update(user user)
        {
            return Dal.update(user);
        }

        public dynamic xq(int id)
        {
            var list = Dal.xs().FirstOrDefault(x => x.id == id);
            return list;
        }

        public dynamic xs(int? fhid, int? fxid)
        {
            var a = Dal.xs();
            var b = Dal1.xs();
            var c = Dal2.xs();
            var list = from x in a
                       join y in b
                       on x.fhid equals y.fhid
                       join w in c
                       on x.fxid equals w.fxid
                       select new
                       {
                           x.id,
                           x.fhid,
                           x.fxid,
                           x.zh,
                           x.mm,
                           y.fhmc,
                           w.fxmc
                           
                       };

            if (fhid != null)
            {
                list = list.Where(x => x.fhid == fhid);
            }
            if (fxid != null)
            {
                list = list.Where(x => x.fxid == fxid);
            }
            return list;
        }
    }
}
